﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using static CoreShop.Library.GlobalEnumVars;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodspecServices : Repository<corecmsfoodspec>, IcorecmsfoodspecServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsfoodspec entity)
        {
            return base.Change<corecmsfoodspec>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodspec> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodspec entity)
        {
            return base.Change<corecmsfoodspec>().Update(a => new corecmsfoodspec()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                foodId = entity.foodId,
                code = entity.code,
                specName = entity.specName,
                onLine = entity.onLine,
                isShare = entity.isShare,
                modifyTime = entity.modifyTime
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodspec> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodspec>().Update(a => new corecmsfoodspec()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    foodId = item.foodId,
                    code = item.code,
                    specName = item.specName,
                    onLine = item.onLine,
                    isShare = item.isShare,
                    modifyTime = item.modifyTime
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodspec>().Update(a => new corecmsfoodspec()
            {
                isDelete = true
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodspec>().Update(a => new corecmsfoodspec()
                {
                    isDelete = true
                }, a => a.id == id);
            }
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodspec> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        /// <summary>
        /// 根据商品类型ID查询类型名称
        /// </summary>
        /// <param name="custoType"></param>
        /// <returns></returns>
        public corecmsfoodspec QueryById(long id)
        {
            corecmsfoodspec food = base.Change<corecmsfoodspec>().GetSingle(a => a.id == id);
            return food;
        }

        /// <summary>
        /// 根据商品类型ID查询类型名称
        /// </summary>
        /// <param name="custoType"></param>
        /// <returns></returns>
        public List<corecmsfoodspec> QueryByCode(string code)
        {
            var where = Expressionable.Create<corecmsfoodspec>();
            where = where.And(a => a.isDelete == false);
            where = where.And(a => a.code == code || a.number == code);
            List<corecmsfoodspec> lst = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.number).With(SqlWith.NoLock).ToList();
            if (lst.Count == 0)
            {
                //一品多码进行查询
                List<long> goodsIds = new corecmsfoodbarcodesServices().AsQueryable()
                    .Where(p=>p.barcode == code).Select(p=>p.spuId).ToList();
                if (goodsIds !=null && goodsIds.Count > 0)
                {
                    var  where1 = Expressionable.Create<corecmsfoodspec>();
                    where1 = where1.And(a => a.isDelete == false);
                    where1 = where1.And(a => goodsIds.Contains(a.foodId));
                    lst = base.AsQueryable().WhereIF(true, where1.ToExpression()).OrderBy(a => a.number).With(SqlWith.NoLock).ToList();
                }
            }
            if (lst.Count == 0)
            {
                //查询商品条码
                List<long> goodsIds = new corecmsfoodServices().AsQueryable()
                    .Where(p => p.number == code).Select(p => p.id).ToList();
                if (goodsIds != null && goodsIds.Count > 0)
                {
                    var where1 = Expressionable.Create<corecmsfoodspec>();
                    where1 = where1.And(a => a.isDelete == false);
                    where1 = where1.And(a => goodsIds.Contains(a.foodId));
                    lst = base.AsQueryable().WhereIF(true, where1.ToExpression()).OrderBy(a => a.number).With(SqlWith.NoLock).ToList();
                }
            }
            return lst;
        }

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>        
        public List<corecmsfoodspec> QueryPageSpecAsync(ref int count, int? pageIndex, int? pageSize, Expressionable<corecmsfoodspec, corecmsfood> where)
        {
            List<corecmsfoodspec> lst = base.AsQueryable().LeftJoin<corecmsfood>((f, s) => f.foodId == s.id)
                .WhereIF(true, where.ToExpression()).Select((f, s) => new corecmsfoodspec {
                    id = f.id,
                    foodId = f.foodId,
                    number = f.number,
                    name = s.name,
                    code = f.code,
                    specName = f.specName,
                    specUnitName = f.specUnitName,
                    posPrice = f.posPrice,
                    vipPrice = f.vipPrice,
                    purPrice = f.purPrice,
                    rmoPrice = f.rmoPrice,
                    periodDay = s.periodDay,
                    place = s.place,
                    weighPlu = f.weighPlu,
                    stand = s.stand,
                }).OrderBy(f => f.number).ToPageList((int)pageIndex, (int)pageSize, ref count).ToList();

            if (lst.Count > 0)
            {
                //获取调价单
                var spuIds = lst.Select(f => f.id).ToList();
                var listPrice = new corecmschgpriceServices().GetChgPriceList(spuIds);
                if (listPrice != null && listPrice.Count > 0)
                {
                    foreach (var item in lst)
                    {
                        var findItem = listPrice.Find(p => p.pdtSpuSpecId == item.id);
                        if (findItem != null) item.posPrice = findItem.newSalePrice;
                    }
                }
            }

            return lst;
        }

        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfood> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, Expressionable<corecmsfoodspec, corecmsfood> where)
        {
            List<corecmsfood> lst = new List<corecmsfood>();
            lst = base.AsQueryable().LeftJoin<corecmsfood>((s, f) => s.foodId == f.id)
                    .WhereIF(true, where.ToExpression())
                    .Select((s, f) => new corecmsfood
                    {
                        id = f.id,
                        number = s.number,
                        name = f.name,
                        byName = f.byName,
                        helpCode = f.helpCode,
                        pinYin = f.pinYin,
                        isChangePrice = f.isChangePrice,
                        isDiscount = f.isDiscount,
                        image = f.image,
                        foodSpecName = s.specName,
                        foodUnitName = s.specUnitName,
                        posPrice = s.posPrice,
                        isHeft = f.isHeft,
                        code = s.code,
                        foodId = s.foodId,
                        foodSpecIds = s.id,
                        foodSpecId = s.id,
                        vipPrice = s.vipPrice,
                        purPrice = s.purPrice,
                        rmoPrice = s.rmoPrice,
                        type = f.type,
                        stand = f.stand,
                        weighPlu = s.weighPlu,
                    }).OrderBy(s => s.number)
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .ToList();

            if (lst.Count > 0)
            {
                //获取调价单
                var spuIds = lst.Select(f => f.foodSpecId).ToList();
                var listPrice = new corecmschgpriceServices().GetChgPriceList(spuIds);
                if (listPrice != null && listPrice.Count > 0)
                {
                    foreach (var item in lst)
                    {
                        var findItem = listPrice.Find(p => p.pdtSpuSpecId == item.foodSpecId);
                        if (findItem != null) item.posPrice = findItem.newSalePrice;
                    }
                }
            }

            return lst;
        }

        #endregion
    }
}
